package cn.bjxy.shishaolong.talk_room.client;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Scanner;

import javax.swing.JFrame;
import javax.swing.JTextArea;

/**
 * 聊天室，客户端服务
 * 
 * @author long
 *
 */
class ClientService {

	// 定义 udpsocket 类引用
	private DatagramSocket ds;
	// 判断聊天服务是否关闭
	private boolean isClosed = false;

	private InetAddress hostAddress;
	private int serverPort;
	private JFrame frame;
	//初始化环境
	public ClientService(int clientPort, String serverIp, int serverPort, JFrame frame ) {
		try {
			ds = new DatagramSocket(clientPort);
			this.hostAddress = InetAddress.getByName(serverIp);
			this.serverPort = serverPort;
			this.frame = frame;
		} catch (SocketException e) {
			e.printStackTrace();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}
	
	public boolean isClosed() {
		return isClosed;
	}
	public void setClosed(boolean isClosed) {
		this.isClosed = isClosed;
	}

	/**
	 * 开启聊天任务
	 * 
	 * @author long
	 *
	 */
/*	class StartTalk implements Runnable {


		public void run() {
			while (!isClosed) {
				try {
					// 2. 将要发送的数据封装到数据包中。
					Scanner scanner = new Scanner(System.in);
					String line = scanner.nextLine();
					// 使用DatagramPacket将数据封装到该对象包中。
					byte[] buf = line.getBytes();

					DatagramPacket dp = new DatagramPacket(buf, buf.length, hostAddress, serverPort);

					// 3. 通过udp的socket服务将数据包发送出去，使用send方法。
					ds.send(dp);
				} catch (IOException e) {
					e.printStackTrace();
					if (isClosed)
						return;
					System.out.println("连接异常！！！");
				}
			}
		}
	}*/

	/**
	 * 开启接受聊天内容任务
	 * 
	 * @author long
	 *
	 */
	class receiveTalk implements Runnable {

		private JTextArea showMessage_tArea; // 显示聊天记录的框

		receiveTalk(JTextArea showMessage_tArea) {   //包访问权限
			this.showMessage_tArea = showMessage_tArea;
		}
		
		private StringBuilder messageInfo = new StringBuilder();
		
		public void run() {
			while (!isClosed) {
				try {
					byte[] buf = new byte[1024];
					DatagramPacket dp = new DatagramPacket(buf, buf.length);
					ds.receive(dp);

					// 得到消息内容
					String message = new String(dp.getData(), 0, dp.getLength());
					 System.out.println(message);
					showMessage_tArea.append(message+"\n");
//					showMessage_tArea.setText(messageInfo.append(message).append("\n").toString());
//					frame.repaint();
				} catch (IOException e) {
					e.printStackTrace();
					if (isClosed)
						return;
					System.err.println("连接异常");
					throw new RuntimeException(e);
				}
			}
		}
	}

	/**
	 * 向服务器发送消息
	 * @param message 要发送的消息内容
	 */
	void sendMessage(String message) {
		if (!isClosed) {
			try {
				// 1. 将要发送的数据封装到数据包中。
				// 使用DatagramPacket将数据封装到该对象包中。
				byte[] buf = message.getBytes();
				DatagramPacket dp = new DatagramPacket(buf, buf.length, hostAddress, serverPort);
				
				// 2. 通过udp的socket服务将数据包发送出去，使用send方法。
				ds.send(dp);
			} catch (IOException e) {
				e.printStackTrace();
				if (isClosed)
					return;
				System.out.println("连接异常！！！");
			}
		}

	}
	
	/**
	 * 关闭聊天服务的后台进程
	 */
	void closeTalk() {
		isClosed = true;
		ds.close();
	}
	/**
	 * 关闭聊天任务
	 * 
	 * @author long
	 *
	 */
	/*class CloseTalk implements Runnable {

		public void run() {
			isClosed = true;
			ds.close();
		}
	}
*/}
